package com.gxuwz.attend.jdbc;

import java.sql.*;

/**
 * JDBC连接管理工具类
 */
public class DbManager {
    //属性-对应连接参数，属性值的初始化通过加载application.properties文件的配置参数进行赋值。
    private String jdbcDriver;
    private String jdbcUrl;
    private String jdbcUser;
    private String jdbcPassword;
    /**
     * 无参构造方法
     */
    public DbManager(){

    }

    /**
     * 获取Connection类型的连接对象
     * @return  conn
     * @throws Exception
     */
    public Connection getConn()throws Exception{
        Connection conn =null;
        try{
            //加载jdbc的驱动类
            Class.forName(this.jdbcDriver);
            conn = DriverManager.getConnection(jdbcUrl,jdbcUser,jdbcPassword);
            return  conn;

        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 关闭连接资源
     * @param rs
     * @param statement
     * @param conn
     * @throws SQLException
     */
    public void close(ResultSet rs, Statement statement, Connection conn)throws SQLException{
        try{
           if(rs!=null){
              rs.close();
           }
            if(statement!=null){
                statement.close();
            }
            if(conn!=null){
                conn.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 重载关闭连接资源的成员方法
     * @param statement
     * @param conn
     * @throws SQLException
     */
    public void close(Statement statement,Connection conn)throws SQLException{
        this.close(null,statement,conn);
    }
    public void close(Connection conn)throws SQLException{
        this.close(null,conn);
    }

    public void setJdbcDriver(String jdbcDriver) {
        this.jdbcDriver = jdbcDriver;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    public void setJdbcUser(String jdbcUser) {
        this.jdbcUser = jdbcUser;
    }

    public void setJdbcPassword(String jdbcPassword) {
        this.jdbcPassword = jdbcPassword;
    }
}
